#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\utility;


init()
{
    precacheModel("prop_flag_neutral");
    precacheModel(maps\mp\gametypes\_teams::getTeamFlagModel(level.zCfg_Attackers)); // recon only flag
    
    createTeleportFlags();
}

createTeleportFlags()
{
    level.zTeleport_Flags = [];
    
    for(i = 0; i < level.zMap_TeleportFlags.size; i++) {
        level.zTeleport_Flags[i] = createTeleportFlag(i);
        
        wait(0.01);
    }
}

createTeleportFlag(teleport_num)
{
    teleport_flag = spawnStruct();
    
    teleport_flag.recon_only = level.zMap_TeleportFlags[teleport_num].recon_only;
    
    teleport_flag.flag_1 = spawn("script_model", level.zMap_TeleportFlags[teleport_num].pos1_origin);
    teleport_flag.flag_1.angles = level.zMap_TeleportFlags[teleport_num].pos1_angles;
    if(teleport_flag.recon_only)
        teleport_flag.flag_1 setModel(maps\mp\gametypes\_teams::getTeamFlagModel(level.zCfg_Attackers));
    else
        teleport_flag.flag_1 setModel("prop_flag_neutral");
    
    teleport_flag.flag_2 = spawn("script_model", level.zMap_TeleportFlags[teleport_num].pos2_origin);
    teleport_flag.flag_2.angles = level.zMap_TeleportFlags[teleport_num].pos2_angles;
    if(teleport_flag.recon_only)
        teleport_flag.flag_2 setModel(maps\mp\gametypes\_teams::getTeamFlagModel(level.zCfg_Attackers));
    else
        teleport_flag.flag_2 setModel("prop_flag_neutral");
    
    teleport_flag.trigger_1 = spawn("trigger_radius", level.zMap_TeleportFlags[teleport_num].pos1_origin + (0, 0, -40), 0, 65, 150);
    teleport_flag.trigger_2 = spawn("trigger_radius", level.zMap_TeleportFlags[teleport_num].pos2_origin + (0, 0, -40), 0, 65, 150);
    
    teleport_flag thread teleportFlagLogic();
    
    return teleport_flag;
}

teleportFlagDelay()
{
    self endon("disconnect");
    
    // disable the class selecting
    self.zClass_CanChange = false;
    
    self.zTeleport_CanUse = false;
    wait(level.zCfg_TeleportDelay);
    self.zTeleport_CanUse = true;
}

teleportFlagLogic()
{
    self endon("death");
    level endon("game_ended");
    
    for(;;) {
        foreach(player in level.players) {
            if(player.team != level.zCfg_Attackers)
                continue;
            
            if(!player.zTeleport_CanUse)
                continue;
            
            if(!player useButtonPressed())
                continue;
            
            if(self.recon_only && !player.isSniper)
                continue;
            
            if(player isTouching(self.trigger_1)) {
                player.zMain_AtBase = false;
                player thread teleportFlagDelay();
                player setOrigin(self.trigger_2.origin + (0, 0, 50));
            } else if(player isTouching(self.trigger_2)) {
                player.zMain_AtBase = true;
                player thread teleportFlagDelay();
                player setOrigin(self.trigger_1.origin + (0, 0, 50));
            }
        }
        
        wait(0.2);
    }
}

